package gov.kqqzyyy.monkey.ydhl.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.Collections;
import java.util.List;

import adapter.SectionListAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import gov.kqqzyyy.monkey.ydhl.R;
import mytools.MyBaseActivity;
import mytools.MyServiceUtil;
import objects.LoginUser;
import objects.Section;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SectionActivity extends MyBaseActivity implements SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {

    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.section_list)
    ListView listView;

    private SectionListAdapter sectionsAdapter;
    private List<Section> mSectionList;

    private Call<String> sectionCall;

    private LoginUser loginUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_section);
        ButterKnife.bind(this);

        sectionsAdapter = new SectionListAdapter(this);
        listView.setAdapter(sectionsAdapter);
        listView.setOnItemClickListener(this);

        setSwipeRefreshLayout(swipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeResources(R.color.holo_green_dark, R.color.holo_green_light, R.color.holo_orange_light, R.color.holo_red_light);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loginUser = getMyApplication().getLoginUser();

        if (loginUser == null) {
            Toast.makeText(getApplicationContext(), "获取登陆数据失败！", Toast.LENGTH_SHORT).show();
        } else {
            doGetSectionList(true, loginUser.getYGDM());
        }

    }

    @Override
    protected void onStop() {
        super.onStop();
        if (sectionCall != null && sectionCall.isExecuted()) {
            sectionCall.cancel();
        }
    }

    @Override
    public void onRefresh() {
        doGetSectionList(false, loginUser.getYGDM());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Section section = mSectionList.get(position);
        if (section != null) {
            sectionsAdapter.setWhich(position);
            getMyApplication().setCurrentSection(section);
            Intent intent = new Intent(getApplicationContext(), PatientListActivity.class);
            startActivity(intent);
        }
    }


    private void doGetSectionList(boolean showProgressBar, String ygdm) {
        if (showProgressBar) {
            showProgressDialog("获取科室中");
        }
        sectionCall = MyServiceUtil.getSectionCall(ygdm);
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                cancelProgressDialog();
                stopRefreshing();
                String result = response.body();
                if (result != null) {
                    Gson gson = new Gson();
                    mSectionList = gson.fromJson(result, new TypeToken<List<Section>>() {
                    }.getType());
                    if (mSectionList != null && mSectionList.size() > 0) {
                        Collections.sort(mSectionList, (arg0, arg1) -> arg0.getKSDM().compareTo(arg1.getKSDM()));
                        sectionsAdapter.setList(mSectionList);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                cancelProgressDialog();
                stopRefreshing();
                Toast.makeText(getApplicationContext(), "获取科室数据失败!", Toast.LENGTH_SHORT).show();
            }
        };
        sectionCall.enqueue(callback);
    }

}
